package com.unknow.first.dapp.admin.account.service.impl;


import static com.unknow.first.dapp.admin.account.constants.AccountErrorConstants.PAIR_CHARGE_IS_NULL;
import static com.unknow.first.dapp.admin.account.constants.AccountErrorConstants.PAIR_PRICE_IS_LT_ZERO;
import static com.unknow.first.dapp.admin.account.constants.AccountErrorConstants.PAIR_PRICE_IS_NULL;
import static com.unknow.first.dapp.admin.account.service.IUpdateSwapPairAfterService.UPDATE_SWAP_PAIR_AFTER_SERVICE_PREFIX;
import static com.unknow.first.dapp.admin.account.service.IUpdateSwapPairBeforeService.UPDATE_SWAP_PAIR_BEFORE_SERVICE_PREFIX;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.unknow.first.dapp.admin.account.dto.FlashSwapPairCreateDTO;
import com.unknow.first.dapp.admin.account.dto.FlashSwapPairUpdateDTO;
import com.unknow.first.dapp.admin.account.mapper.AddressCoinConfig;
import com.unknow.first.dapp.admin.account.mapper.FlashSwapPair;
import com.unknow.first.dapp.admin.account.mapper.FlashSwapPair.FlashSwapPairBuilder;
import com.unknow.first.dapp.admin.account.mapper.FlashSwapPairMapper;
import com.unknow.first.dapp.admin.account.service.AddressCoinConfigService;
import com.unknow.first.dapp.admin.account.service.FlashSwapPairService;
import com.unknow.first.dapp.admin.account.service.IUpdateSwapPairAfterService;
import com.unknow.first.dapp.admin.account.service.IUpdateSwapPairBeforeService;
import com.unknow.first.dapp.admin.account.utils.FlashSwapUtil;
import java.math.BigDecimal;
import java.util.List;
import org.cloud.utils.SpringContextUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.Assert;

@Service
public class FlashSwapPairServiceImpl extends ServiceImpl<FlashSwapPairMapper, FlashSwapPair> implements FlashSwapPairService {

    @Override
    public int insertSelective(FlashSwapPair record) {
        return baseMapper.insertSelective(record);
    }

    @Override
    public int updateByPrimaryKeySelective(FlashSwapPair record) {
        return baseMapper.updateByPrimaryKeySelective(record);
    }

    @Override
    public int updateBatch(List<FlashSwapPair> list) {
        return baseMapper.updateBatch(list);
    }

    @Override
    public int batchInsert(List<FlashSwapPair> list) {
        return baseMapper.batchInsert(list);
    }

    @Autowired
    AddressCoinConfigService coinConfigService;

    @Override
    public int create(FlashSwapPairCreateDTO pairCreateDTO) throws Exception {

        Assert.notNull(pairCreateDTO.getPrice(), PAIR_PRICE_IS_NULL.code);
        Assert.isTrue(pairCreateDTO.getPrice().compareTo(BigDecimal.ZERO) > 0, PAIR_PRICE_IS_LT_ZERO.code);

        AddressCoinConfig coin1Info = coinConfigService.getOne(
            Wrappers.<AddressCoinConfig>lambdaQuery().eq(AddressCoinConfig::getCoinId, pairCreateDTO.getCoin1Id()));
        AddressCoinConfig coin2Info = coinConfigService.getOne(
            Wrappers.<AddressCoinConfig>lambdaQuery().eq(AddressCoinConfig::getCoinId, pairCreateDTO.getCoin2Id()));

        FlashSwapPairBuilder<?, ?> flashSwapPairBuilder = FlashSwapPair.builder();
        flashSwapPairBuilder.coin1Name(coin1Info.getCoinName()).coin1Symbol(coin1Info.getCoinSymbol());
        flashSwapPairBuilder.coin2Name(coin2Info.getCoinName()).coin2Symbol(coin2Info.getCoinSymbol());

        FlashSwapPair flashSwapPairCreate = flashSwapPairBuilder.build();
        BeanUtils.copyProperties(pairCreateDTO, flashSwapPairCreate);
        return this.insertSelective(flashSwapPairCreate);
    }

    @Override
    @Transactional(rollbackFor = Throwable.class)
    public Integer update(FlashSwapPairUpdateDTO swapPairUpdateDTO) throws Exception {

        Assert.notNull(swapPairUpdateDTO.getChargePercent(), PAIR_CHARGE_IS_NULL.code);
        Assert.notNull(swapPairUpdateDTO.getPrice(), PAIR_PRICE_IS_NULL.code);
        Assert.isTrue(swapPairUpdateDTO.getPrice().compareTo(BigDecimal.ZERO) > 0, PAIR_PRICE_IS_LT_ZERO.code);

        FlashSwapPair flashSwapPairUpdate = new FlashSwapPair();
        BeanUtils.copyProperties(swapPairUpdateDTO, flashSwapPairUpdate);

        final String pairName = FlashSwapUtil.single().getPairNameById(swapPairUpdateDTO.getSwapPairId());

        IUpdateSwapPairBeforeService beforeService = SpringContextUtil.getBean(UPDATE_SWAP_PAIR_BEFORE_SERVICE_PREFIX + pairName);

        if (beforeService != null) {
            beforeService.execute(swapPairUpdateDTO);
        }
        int result = this.updateByPrimaryKeySelective(flashSwapPairUpdate);
        IUpdateSwapPairAfterService afterService = SpringContextUtil.getBean(UPDATE_SWAP_PAIR_AFTER_SERVICE_PREFIX + pairName);
        if (afterService != null) {
            afterService.execute(swapPairUpdateDTO);
        }
        return result;
    }


}
